package com.allen.app.common.domain;

import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModelProperty;
import java.util.Collections;
import java.util.List;
import lombok.Data;

/**
*
* Description: 分页返回对象
*
* @author Limiaojun
* @date 2019/3/28 17:59
**/
@Data
public class PageResult<T> {

    @ApiModelProperty(notes = "当前页")
    private Long page;

    @ApiModelProperty(notes = "总记录数")
    private Long total;

    @ApiModelProperty(notes = "当前返回记录数")
    private Long records;

    @ApiModelProperty(notes = "总分页数")
    private Long allPages;

    @ApiModelProperty(notes = "数据")
    private List<T> rows = Collections.emptyList();

    /**
     * Description:构建分页返回对象
     *
     * @param rows 返回list对象
     * @return PagingResultDto<T>
     * @author Limiaojun
     * @date 2018-05-31 09:27
     */
    public static <T> PageResult<T> of(List<T> rows) {
        PageResult<T> pageResult = new PageResult<>();
        PageInfo pageInfo = new PageInfo(rows);
        pageResult.setPage(Convert.toLong(pageInfo.getPageNum()));
        pageResult.setTotal(Convert.toLong(pageInfo.getTotal()));
        pageResult.setRecords(Convert.toLong(pageInfo.getSize()));
        pageResult.setAllPages(Convert.toLong(pageInfo.getPages()));
        pageResult.setRows(rows);
        return pageResult;
    }
}
